#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
This Module is for drawing a StereoDiagrams of the fractures
"""

import sys
from optparse import OptionParser
import fr_parser
from plane import Plane

def stress_parse(sfile):    
    f = open(sfile, 'r')	
    lines = f.readlines(); f.close
    line = lines[1].expandtabs().split()
    return [Plane(int(line[0]), int(line[1])), \
            Plane(int(line[2]), int(line[3])), \
            Plane(int(line[4]), int(line[5]))]

def readdensity(fname):
    f = open(fname, 'r')	
    lines = f.readlines(); f.close
    result = []
    size = float(lines.pop(0))
    for line in lines:
        valyes = [float(x) for x in line.split()]
        result.append(valyes)
    return result, size

def readfdensity(fname):
    f = open(fname, 'r')
    lines = f.readlines(); f.close
    result = []
    for line in lines:
        valyes = [float(x) for x in line.split()]
        result.append(valyes)
    return result


# Parsing of an options
optparser= OptionParser()
optparser.add_option('-o', '--output', dest='output',
                     help='Name of a output file')
optparser.add_option('-t', '--title', dest='title',
                     help='Title for the plot',
                     default='')
optparser.add_option('-c', '--comment', dest='comment',
                     help='Comment to be displaied on a plot',
                     default='')                     
optparser.add_option('-n', dest='count',
                     help='Count the number of the planes and plot it below \
                     the plot',
                     action='store_true',
                     default=False)                     
optparser.add_option('-d', dest='dfile',
					help='Plot the density grid, which store in the file name, on the background')
optparser.add_option('-f', dest='ffile',
					help='Plot the flat density grid, which store in the file name, on the background')				
optparser.add_option('-r', dest='normals',
					help='Plot normals instead of planes (soft)', 
					action='store_true',
                    default=False)				
optparser.add_option('-y', dest='normals_hard',
					help='Plot normals instead of planes (hard)', 
					action='store_true',
                    default=False)				
optparser.add_option('-k', dest='allTR',
					help='Allways read inpit directions as TR when', 
					action='store_true',
                    default=False)				
optparser.add_option('-u', dest='upper',
					help='Upper hemisphere', 
					action='store_true',
                    default=False)				
optparser.add_option('-s', dest='stress',
					help='Name of a file with stress axes')
optparser.add_option('-b', dest='blank',
					help='Blank mode. No title, no annotation, no comments', 
					action='store_true',
                    default=False)
optparser.add_option('-R', dest='radius',
					help='Radius of the plot', type='int' ,
                    default=400)
optparser.add_option('-w', dest='whitespaces', type='float',
                    help='Whitespaces for title and annotation',
                    default=2.4)					
options, args = optparser.parse_args(sys.argv[1:])
file_name= options.output

lines = sys.stdin.read().split('\n')

# Parse the density grid file
density, size, fdensity = None, None, None
if options.dfile:
	density, size = readdensity(options.dfile)

if options.ffile:
    fdensity = readfdensity(options.ffile)   


# Parsig of a data file
data=[]
for line in lines:
    val = fr_parser.parse_line(line)
    if val: data.append(val)    


if options.upper:
    hemisphere='upper'
else:
    hemisphere='lower'

stress_commands=[]; stress=None

# Guuessing a command sequenses to plot the diagramm
import str_canvas
commands = str_canvas.ReturnCommands(data, options.normals, options.normals_hard, options.allTR, hemisphere)

if options.stress:
    stress = stress_parse(options.stress)
    #stress_commands = str_canvas.ReturnCommands(stress, hemisphere=hemisphere, isstress=True)
count=0
if options.count:
    count=len(data)

# Plotting
import str_cairo_file
str_cairo_file.plot_diagram(commands, file_name, options.radius, options.title, options.comment, count, density, size, fdensity, stress_commands, stress, options.blank, options.whitespaces)
